package com.zzyl.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.io.Resources;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 角色前端控制器
 */
@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;



    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "角色分页",notes = "角色分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto"),
        @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
        @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    public ResponseResult findRoleVoPage(
                                    @RequestBody RoleDto roleDto,
                                    @PathVariable("pageNum") int pageNum,
                                    @PathVariable("pageSize") int pageSize) {

        PageResponse<RoleVo> roleVoList = roleService.findRoleVoPage(roleDto, pageNum, pageSize);
        return ResponseResult.success(roleVoList);
    }


    /**
     *  保存角色
     * @param roleDto 角色DTO对象
     * @return RoleVo
     */
    @PutMapping
    @ApiOperation(value = "角色添加",notes = "角色添加")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    public ResponseResult createRole(@RequestBody RoleDto roleDto) {
        roleService.createRole(roleDto);
        return ResponseResult.success();

    }

    @ApiOperation(value = "根据角色修改")
    @PatchMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto"),
    })
    public ResponseResult updataRole(@RequestBody RoleDto roleDto){
       Boolean success = roleService.updataRole(roleDto);
        return ResponseResult.success(success);

    }


    @ApiOperation(value = "根据角色查询选中的资源数据")
    @GetMapping("/find-checked-resources/{roleId}")
    public ResponseResult findCheckedResources(@PathVariable("roleId") Long roleId){
        Set<String>ResourcesIds =roleService.findCheckedResources(roleId);
        return ResponseResult.success(ResourcesIds);

    }

    @ApiOperation(value = "角色下拉框")
    @PostMapping("/init-roles")
    public ResponseResult initRoles(){
        List<RoleVo> roleVos = roleService.initRoles();
        return ResponseResult.success(roleVos);
    }

}